﻿-- Author      : Kurapica
-- Create Date : 8/22/2008 23:25
     
-- ScrollForm
do
	-- Check Version
	local version = 1
	if not IGAS:RegisterFile("IGAS.GUI.Widget.ScrollForm", version) then
		return
	end

    local getmetatable = getmetatable
    local pairs = pairs
    local ipairs = ipairs
    local type = type
    local error = error
    local CreateFrame = IGAS.GUI.CreateFrame
    
	local _WidgetName = "ScrollForm"
    local _Base = "Frame"

	local _GUI = IGAS.GUI
	local _Widget = IGAS.GUI.Widget
    local _BaseFunc = IGAS.GUI.Base.Func
	local _PropertyType = IGAS.GUI.Base.PropertyType   
	
    -- Scripts
    local _FrameBackdrop = {
        bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
        edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
        tile = true, tileSize = 16, edgeSize = 16,
        insets = { left = 3, right = 3, top = 3, bottom = 3 }
    }
    
	local function OnValueChanged(self, value)
        local frame, container = self.Parent, self.Parent:GetChild("Container")
		local viewheight, height = frame.Height, container.Height
		local offset = value
        
		if viewheight > height then
			offset = 0
		elseif offset > height - viewheight then
			offset = height - viewheight
		end
        
		container:ClearAllPoints()
		container:SetPoint("TOPLEFT",frame,"TOPLEFT",0,offset)
		container:SetPoint("TOPRIGHT",frame,"TOPRIGHT",-18,offset)
	end
	
	local function FixScroll(self)
		local bar = self:GetChild("Bar")
		local viewheight, height = self.Height, self:GetChild("Container").Height
		local curvalue = bar.Value
        
		if viewheight >= height then
            bar:Disable()
			bar:Hide()
			bar:SetValue(0)
		else
			bar:Show()
			local maxValue = height - viewheight
            if curvalue > maxValue then curvalue = maxValue end
            bar:SetMinMaxValues(0,maxValue)
			bar:SetValue(curvalue)
            bar:Enable()
		end
	end
    
    local function OnMouseWheel(self, delta)
        local scrollBar = self:GetChild("Bar")
        if ( delta > 0 ) then
            if self.Parent.ValueStep == 0 then
                scrollBar:SetValue(scrollBar:GetValue() - (scrollBar:GetHeight() / 2))
            else
                scrollBar:SetValue(scrollBar:GetValue() - self.Parent.ValueStep)
            end
        else
            if self.Parent.ValueStep == 0 then
                scrollBar:SetValue(scrollBar:GetValue() + (scrollBar:GetHeight() / 2))
            else
                scrollBar:SetValue(scrollBar:GetValue() + self.Parent.ValueStep)
            end
        end
    end
    
    local function OnSizeChanged(self)
        if self.Parent.ValueStep == 0 then
            self:GetChild("Bar").ValueStep = floor(self:GetChild("Bar").Height / 2)
        end
        FixScroll(self)
    end
    
    local function OnSizeChanged2(self)
        FixScroll(self.Parent)
    end
    
	-- ScriptType
	local _ScriptType = {
	}
    	
	-- FuncProxy
	local _FuncProxy = {
    }
    
	-- Property
	local _Property = {
        -- Container
        ["Container"] = {
			["Get"] = function(self)
				return self:GetChild("ScrollFrame"):GetChild("Container")
			end,
			
			["Type"] = _PropertyType.Object,
        },
        
        -- ContainerHeight
        ["ContainerHeight"] = {
			["Set"] = function(self, height)
				self:GetChild("ScrollFrame"):GetChild("Container").Height = height
			end,
			
			["Get"] = function(self)
				return self:GetChild("ScrollFrame"):GetChild("Container").Height
			end,
			
			["Type"] = _PropertyType.Number,
		},        
        
        -- ValueStep
        ["ValueStep"] = {
			["Set"] = function(self, value)
				self.__ValueStep = value or 0
                if value > 0 then
                    self.__ValueStep = value
                    self:GetChild("ScrollFrame"):GetChild("Bar").ValueStep = value
                elseif value <= 0 then
                    self.__ValueStep = 0
                    self:GetChild("ScrollFrame"):GetChild("Bar").ValueStep = floor(self:GetChild("Bar").Height / 2)
                end
			end,
			
			["Get"] = function(self)
				return self.__ValueStep or 0
			end,
			
			["Type"] = _PropertyType.Number,
		},
		
		-- Value
        ["Value"] = {
			["Set"] = function(self, value)
				self:GetChild("ScrollFrame"):GetChild("Bar").Value = value
			end,
			
			["Get"] = function(self)				
				return self:GetChild("ScrollFrame"):GetChild("Bar").Value
			end,
			
			["Type"] = _PropertyType.Number,
		},
	}
	
    -- New
    local _New = function(parent)
    
        local back = CreateFrame("Frame",nil,parent)
        back:SetBackdrop(_FrameBackdrop)
		back:SetBackdropColor(0, 0, 0)
		back:SetBackdropBorderColor(0.4, 0.4, 0.4)
        back.MouseWheelEnabled = true
		back.MouseEnabled = true
        back.Height = 400
        back.Width = 200
        
		local frame = CreateFrame("ScrollFrame","ScrollFrame",back)
        frame:SetPoint("TOPLEFT", back, "TOPLEFT", 5, -6)
		frame:SetPoint("BOTTOMRIGHT", back, "BOTTOMRIGHT", -5, 6)
        frame.MouseWheelEnabled = true
		frame.MouseEnabled = true
        
        local slider = CreateFrame("ScrollBar", "Bar", frame)
        slider:SetMinMaxValues(0,0)
        slider.Value = 0
        slider.ValueStep = 10
        
        local container = CreateFrame("Frame", "Container", frame)
        frame:SetScrollChild(container)
		container:SetPoint("TOPLEFT",frame,"TOPLEFT",0,0)
		container:SetPoint("TOPRIGHT",frame,"TOPLEFT",-18,0)
        container.Height = back.Height - 12
        container.Back = back
        
        frame.OnSizeChanged = OnSizeChanged
        frame.OnMouseWheel = OnMouseWheel
        container.OnSizeChanged = OnSizeChanged2
        slider.OnValueChanged = OnValueChanged
        
        frame.__ValueStep = 10
        FixScroll(frame)
        
		-- Don't move this code
		slider.Value = 10
		slider.Value = 0
		
        return back
    end
	
    local _WidgetInfo = {
        ["WidgetName"] = _WidgetName,
        ["Base"] = _Base,
        ["FuncProxy"] = _FuncProxy,
        ["Property"] = _Property,
        ["ScriptType"] = _ScriptType,
        ["New"] = _New,
    }
    
    _Widget[_WidgetName] = _GUI.NewWidget(_WidgetInfo)    
end